package com.example.hyg_veintype.view.veinTypeImgParamsDialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.data.local.chart.ChartBean;
import com.example.hyg_veintype.base.baseadapter.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: yyw
 * time: 2020/12/18 10:56
 * desc:脉图参数对话框
 */
public class VeinTypeImgParamsDialog extends Dialog {


    Context context;
    ViewPager2 vp;

    VeinTypeImgParamsVpAdapter vpAdapter;//脉图参数适配器

    List<ChartBean> list = new ArrayList<>();//最佳脉图数据


    public VeinTypeImgParamsDialog(Context context,List<ChartBean> list) {
        this(context, R.style.commonDialog);
        this.context = context;
        this.list=list;

        create();
    }

    public VeinTypeImgParamsDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.veintype_dialog_veubtype_img_params, null);
        setContentView(view);

        vp=view.findViewById(R.id.vp);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.9); // 高度设置为屏幕
        lp.height = (int) (d.heightPixels * 0.9);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //去掉背景色
        dialogWindow.setAttributes(lp);

        vpAdapter = new VeinTypeImgParamsVpAdapter(list, context);
        vp.setAdapter(vpAdapter);
//        vp.setPageTransformer(new ViewPager2.PageTransformer() {
//            @Override
//            public void transformPage(@NonNull View page, float position) {
//                //position为将要呈现在屏幕的页面，-的是上一个，+的是下一个
//                Float absPos=Math.abs(position);
//                Float scaleX=1-absPos;
//                Float scaleY=1-absPos;
//                page.setScaleX(scaleX);
//                page.setScaleY(scaleY);
//                page.setAlpha(scaleX);
//
//            }
//        });
        vpAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                dismiss();
            }
        });

        vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);


            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    public void scrollToIndex(int index){
        vp.setCurrentItem(index,false);
    }
}
